package study.算法Algorithm.排序算法.冒泡;

import java.util.Arrays;

public class BubbleSort {

    public static void main(String[] args) {

        m01();
        m02();
        m03();

    }

    private static void m03() {
        int arr[] = {3,9,-1,10,2};
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - 1; j++) {
                int temp = 0;
                temp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = temp;
            }
            System.out.println("第"+(i+1)+"趟排序");
            System.out.println(Arrays.toString(arr));
        }
    }

    private static void m02() {
        int arr[] = {3,9,-1,10,-2};
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - 1; j++) {
                int temp = 0;
                temp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = temp;
            }
            System.out.println("第"+(i+1)+"趟排序");
            System.out.println(Arrays.toString(arr));
        }
    }

    private static void m01() {
        //冒泡排序的时间复杂度  O(n^2)
        int arr[] = {3, 9, -1, 10, -2};
        for (int i = 0; i < arr.length - 1; i++) {
            int temp = 0;  //临时变量   用来辅助交换
            for (int j = 0; j < arr.length - 1; j++) {
                //如果前面的数比后面的数大  则交换
                if (arr[j] > arr[j + 1]) {
                    temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
            System.out.println("第"+(i+1)+"趟排序");
            System.out.println(Arrays.toString(arr));
        }
    }
}
